package com.zrf.base.knowledge.concurrent.demo;

import org.apache.commons.lang3.RandomUtils;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @date 2020/3/21
 * @AUTHER zhouruifeng
 */
public class CountDownLatchDemo {


    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(2);

        ExecutorService executorService = Executors.newFixedThreadPool(10);
        executorService.submit(()->{
            try {
                TimeUnit.SECONDS.sleep(RandomUtils.nextInt(0,10));
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }finally {
                countDownLatch.countDown();
            }
        });

        executorService.submit(()->{
            try {
                TimeUnit.SECONDS.sleep(RandomUtils.nextInt(0,10));
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }finally {
                countDownLatch.countDown();
            }
        });

        countDownLatch.await();
        executorService.shutdown();
    }

}
